<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace plugins\pay_zhl\controller; //Demo插件英文名，改成你的插件英文就行了

use cmf\controller\PluginAdminBaseController;

class AdminIndexController extends PluginAdminBaseController
{

    public function _initialize()
    {
        $adminId = cmf_get_current_admin_id();//获取后台管理员id，可判断是否登录
        if (!empty($adminId)) {
            $this->assign("admin_id", $adminId);
        } else {
            $this->error('未登录');
        }
    }

    /**
     * 第三方支付管理
     * @adminMenu(
     *     'name'   => '第三方支付管理',
     *     'parent' => 'admin/Plugin/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '第三方支付管理',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        return $this->fetch('/admin_index');
    }
    /**
     * 第三方支付配置
     * @adminMenu(
     *     'name'   => '第三方支付配置',
     *     'parent' => 'AdminIndex/index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '第三方支付配置',
     *     'param'  => ''
     * )
     */
    public function edit()
    {
        $param=$this->request->param();
        $type=$param['type'];
        if($type=='wechat'){
            $type_name="微信支付";
            $wechat_pay = cmf_get_option('wechat_pay');
            $this->assign('wechat',$wechat_pay);
        }
        if($type=='ali'){
            $type_name="支付宝支付";
        }
        $this->assign('type_name',$type_name);
        return $this->fetch('/edit');
    }
    /**
     * 微信支付编辑
     * @adminMenu(
     *     'name'   => '微信支付编辑',
     *     'parent' => 'AdminIndex/index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '微信支付编辑',
     *     'param'  => ''
     * )
     */
    public function editPost_wechat()
    {
        $param=$this->request->param();
        cmf_set_option('wechat_pay', $param);
        $str="<?php \n class WxPayConfig{ \n";
        foreach ($param as $key=>$value){
            $str=$str."const ".$key." = '".$value."';\n";
        }
        $str=$str."const CURL_PROXY_HOST = '0.0.0.0';\n const CURL_PROXY_PORT = 0;\n const REPORT_LEVENL = 1;\n}";

        $path=PLUGINS_PATH."pay_zhl/wechat/lib/WxPay.Config2.php";
        if(file_put_contents($path,$str)){
            $this->success('保存成功！');
        }else{
            $this->success('保存失败！');
        }

    }
}
